﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace DocClient
{
    class Training
    {
        private int stapje = 50;
        private DoctorForm _form;

        private int gewicht;
        private int age;
        private int gender;

        private int counter = 0;
        private int _id;
        int teller = 0;
        List<int> heartbeat = new List<int>();
        Timer t;
        Timer t2;

        private double uitslag;

        public Training(int gender, int age, int gewicht, int id, DoctorForm form)
        {
            this.gender = gender;
            this.gewicht = gewicht;
            this.age = age;

            _form = form;
            _id = id;

            form.secureConnection.SendObject("chat: " + "This is the start of the Astrand test." + ":" + id);
            Thread.Sleep(10);
            form.secureConnection.SendObject("chat: " + "First we start with the warming up."  + ":" + id);
            Thread.Sleep(10);
            form.secureConnection.SendObject("chat: " + "Keep your RPM on 60." + ":" + id);
            Thread.Sleep(10);
            form.secureConnection.SendObject("chat: " + "When you here the beep, th test starts"  + ":" + id);
            Thread.Sleep(5000);
            form.secureConnection.SendObject("BikeCommand:" + id + ":RS:" + "");
            Thread.Sleep(10);
            form.secureConnection.SendObject("BikeCommand:" + id + ":CM:");
            Thread.Sleep(5000);
            form.secureConnection.SendObject("chat: " + "Bike Reset complete, Start cycling" + ":" + id);
            Boolean check = true;
            int Count = 0;

            while (form.hearthbeat < 120) // warming up
            {
                if (form.rpm < 60 && check == true)
                {
                    form.secureConnection.SendObject("chat: " + "go faster, 60 rpm is necessary!" + ":" + id);
                    check = false;
                }
                else if (form.rpm >= 60)
                {
                    check = true;
                    Count++;
                    //form.secureConnection.SendObject("chat: " + "Count =" + Count + ":" + id);
                    Thread.Sleep(500);

                    if (Count == 60)
                    {
                        form.secureConnection.SendObject("BikeCommand:" + id + ":CM:");
                        Thread.Sleep(10);
                        form.secureConnection.SendObject("BikeCommand:" + id + ":PW:" + stapje);
                        stapje = stapje + 50;
                        Thread.Sleep(10);
                        form.secureConnection.SendObject("chat: " + "Increasing power with 50" + ":" + id);
                        Count = 0;
                    }
                }

            }



            form.secureConnection.SendObject("BikeCommand:" + id + ":RS:" + "");
            Thread.Sleep(10);
            form.secureConnection.SendObject("BikeCommand:" + id + ":CM:");
            Thread.Sleep(10);
            form.secureConnection.SendObject("BikeCommand:" + id + ":PW:" + stapje);
            Thread.Sleep(10);
            form.secureConnection.SendObject("chat: " + "The warming up is over, the test starts now" + ":" + id);
            Thread.Sleep(10);
            form.secureConnection.SendObject("chat: " + "Keep cycling on 60RPM for 6min." + ":" + id);
            Thread.Sleep(10);
            form.secureConnection.SendObject("chat: " + "The chat wil inform you when the test is over." + ":" + id);
            Thread.Sleep(10);


            t = new Timer(TimerCallback, null, 0, (60000));
        }

        private void TimerCallback(object state)
        {
            _form.secureConnection.SendObject("chat: " + "Already " + teller + " min" + ":" + _id);
            teller = teller + 1;
            if (teller == 2) //TODO FIX@@@#!#!
            {
                t2 = new Timer(calculation, null, 0, (60000));
                t.Dispose();
            }
        }

        private void calculation(object state)
        {
            heartbeat.Add(_form.hearthbeat);
            if (counter == 1)
            {
                int gemmidelde = (Int32) heartbeat.Average();
                _form.secureConnection.SendObject("chat: " + "Test succesfull, heartbeat is " + gemmidelde.ToString() + " BPM" + ":" + _id);
                t2.Dispose();
                calculater(gemmidelde);
            }
            counter++;
        }

        public void calculater(int hartslaggen)
        {
            uitslag = (132.853 - (0.0769 * gewicht ) - (0.3877 * age) + (6.315 * gender) - (3.2649 * 6) - (0.1565 * hartslaggen));
            _form.secureConnection.SendObject("chat: " + "Score is " + uitslag.ToString() + ":" + _id);
            Thread.Sleep(10);
            _form.secureConnection.SendObject("chat: " + "Cooling down, relax and slow down " + ":" + _id);
            Thread.Sleep(10);
            berekenUitslag();
        }

        public void berekenUitslag()
        {
            if (gender == 1) //if man
            {
                if (age <= 20)
                {
                    if (uitslag <= 38) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 38 && uitslag <= 44) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 44 && uitslag <= 53) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 53 && uitslag <= 57) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 58) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age > 20 && age <= 29)
                {
                    if (uitslag <= 38) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 38 && uitslag <= 43) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 43 && uitslag <= 51) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 51 && uitslag <= 56) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 57) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age >= 30 && age <= 39)
                {
                    if (uitslag <= 34) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 34 && uitslag <= 39) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 39 && uitslag <= 47) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 47 && uitslag <= 51) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 51) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }


                if (age >= 40 && age <= 49)
                {
                    if (uitslag <= 30) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 30 && uitslag <= 35) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 35 && uitslag <= 43) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 43 && uitslag <= 47) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 47) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age >= 50 && age <= 59)
                {
                    if (uitslag <= 25) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 25 && uitslag <= 31) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 31 && uitslag <= 39)_form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 39 && uitslag <= 43) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 43) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age > 59)
                {
                    if (uitslag <= 21) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 21 && uitslag <= 26) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 26 && uitslag <= 35) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 35 && uitslag <= 39) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 39) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }
            }
            else if (gender == 0)
            {
                if (age <= 20)
                {
                    if (uitslag <= 28) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 28 && uitslag <= 35) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 35 && uitslag <= 45) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 45 && uitslag <= 60) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 60) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age > 20 && age <= 29)
                {
                    if (uitslag <= 28) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 28 && uitslag <= 34) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 34 && uitslag <= 43) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 43 && uitslag <= 48) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 48) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age >= 30 && age <= 39)
                {
                    if (uitslag <= 27) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 27 && uitslag <= 33) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 33 && uitslag <= 41) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 41 && uitslag <= 47) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 47) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }


                if (age >= 40 && age <= 49)
                {
                    if (uitslag <= 25) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 25 && uitslag <= 31) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 31 && uitslag <= 40) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 40 && uitslag <= 45) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 45) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age >= 50 && age <= 59)
                {
                    if (uitslag <= 21) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 21 && uitslag <= 28) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 28 && uitslag <= 36) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 36 && uitslag <= 41) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 41 ) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

                if (age > 59)
                {
                    if (uitslag <= 19) _form.secureConnection.SendObject("chat: " + "Your condition is Weak " + ":" + _id);
                    if (uitslag > 19 && uitslag <= 25) _form.secureConnection.SendObject("chat: " + "Your condition is Moderate " + ":" + _id);
                    if (uitslag > 25 && uitslag <= 33) _form.secureConnection.SendObject("chat: " + "Your condition is Average " + ":" + _id);
                    if (uitslag > 33 && uitslag <= 39) _form.secureConnection.SendObject("chat: " + "Your condition is Good " + ":" + _id);
                    if (uitslag > 39) _form.secureConnection.SendObject("chat: " + "Your condition is Very Good " + ":" + _id);
                }

            }
            else _form.secureConnection.SendObject("chat: " + "Entered gender wrong" + ":" + _id);
        }

    }

}
